//
//  PosterCollectionView.m
//  HuiWenMovieApp
//
//  Created by ios on 15/7/24.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "PosterCollectionView.h"
#import "UIViewExt.h"
#import "PosterCollectionViewCell.h"
#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height

@implementation PosterCollectionView

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self)
    {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self registerClass:[PosterCollectionViewCell class] forCellWithReuseIdentifier:@"posterCell"];
    }
    return  self;

}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_models count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PosterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"posterCell" forIndexPath:indexPath];
    
    cell.model = _models[indexPath.row];
    
    
    return cell;
}

- (void)setModels:(NSArray *)models
{
    _models = models;
    [self reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(_cellWidth , _cellHeight);
    
    
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    
    
    return UIEdgeInsetsMake(0, (kwth-_cellWidth)/2, 0, (kwth-_cellWidth)/2);
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGFloat width = kwth*3/4;
    NSLog(@"%f",width);
    CGFloat x = targetContentOffset->x; //+182.25 ;
    NSLog(@"%f",x);
    
    NSInteger index = x / _cellWidth;
    NSLog(@"%li",index);
    
    targetContentOffset->x = index *_cellWidth;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PosterCollectionViewCell *cell = (PosterCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell flip];
}


@end
